package com.qf.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.util.Date;


@Aspect
@Configuration
public class LoggerConfig {
    /**
     * 定义日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConfig.class);

    /**
     * 声明切点表达式
     */
    @Pointcut("execution(* *.*.service.impl.*.*(..))")
    private void pt1() {
    }

    /**
     * 环绕通知
     *
     * @return
     */
    @Around(value = "pt1()")
    public Object logger(ProceedingJoinPoint joinPoint) {
        //    开始记录日志
        LOGGER.debug("----------开始记录日志----------");
        LOGGER.debug("当前时间为==>" + new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
        //记录:当前时间_执行方法的名称
        Signature signature = (MethodSignature) joinPoint.getSignature();
        LOGGER.debug("当前执行的方法为:" + signature.getName());
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            LOGGER.debug("方法出现了异常:" + throwable.getMessage());
        } finally {
            //结束记录日志信息
            LOGGER.debug("----------结束记录日志----------");
            LOGGER.debug("当前时间为==>" + new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
            return proceed;
        }
    }
}
